package drawables;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;


public class WallDrawable{
	
	private static final int WALL_THICKNESS = 10;
	private static final Color WALL_COLOR_DARK = new Color(100, 100, 100);
	public int startX, startY, endX, endY;
	private GeneralPath wallShape;
	
	public WallDrawable(int startX, int startY, int endX, int endY) {
		this.startX = startX;
		this.startY = startY;
		this.endX = endX;
		this.endY = endY;
		
		//create wall edges
		constructWall();
	}
	
	public void switchEnds(){
		int temp = startX;
		startX = endX;
		endX = temp;
		temp = startY;
		startY = endY;
		endY = temp;
	}
	
	public void draw(Graphics2D g2d){
		g2d.setColor(WALL_COLOR_DARK);
		g2d.fill(wallShape);
		//draw caps
		g2d.fillOval(startX-WALL_THICKNESS/2, startY-WALL_THICKNESS/2, WALL_THICKNESS, WALL_THICKNESS);
		g2d.fillOval(endX-WALL_THICKNESS/2, endY-WALL_THICKNESS/2, WALL_THICKNESS, WALL_THICKNESS);
	}
	
	public boolean intersects(float x, float y){
		return wallShape.contains(x, y);
	}
	
	public boolean intersects(Rectangle2D bounds){
		return wallShape.intersects(bounds);
	}
	
	private void constructWall(){
		int dX = endX - startX;
		int dY = endY - startY;
		// line length
		double lineLength = Math.sqrt(dX * dX + dY * dY);

		double scale = (double)(WALL_THICKNESS) / (2 * lineLength);

		// The x,y increments from an endpoint needed to create a rectangle...
		double ddx = -scale * (double)dY;
		double ddy = scale * (double)dX;
		ddx += (ddx > 0) ? 0.5 : -0.5;
		ddy += (ddy > 0) ? 0.5 : -0.5;
		float dx = (float)ddx;
		float dy = (float)ddy;

		// Now we can compute the corner points...
		float xPoints[] = new float[4];
		float yPoints[] = new float[4];

		xPoints[0] = startX + dx; yPoints[0] = startY + dy;
		xPoints[1] = startX - dx; yPoints[1] = startY - dy;
		xPoints[2] = endX - dx; yPoints[2] = endY - dy;
		xPoints[3] = endX + dx; yPoints[3] = endY + dy;
		
		//create drawable shape
		wallShape = new GeneralPath();
		wallShape.moveTo(xPoints[0], yPoints[0]);
		for(int i=1; i<4; i++)
			wallShape.lineTo(xPoints[i], yPoints[i]);
	}
}
